package com.stylefeng.guns.modular.biz.service.impl;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.common.persistence.dao.*;
import com.stylefeng.guns.common.persistence.model.*;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.biz.service.IOrderService;
import org.springframework.stereotype.Service;
import com.stylefeng.guns.modular.biz.service.IWorkNoticeService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 工单通知Service
 *
 * @author fengshuonan
 * @Date 2017-10-22 11:38:15
 */
@Service
public class WorkNoticeServiceImpl implements IWorkNoticeService {

    @Resource
    WorkNoticeMapper workNoticeMapper;

    @Resource
    IOrderService orderService;

    @Resource
    private OrderInfoMapper orderInfoMapper;

    @Resource
    OrderItemMapper orderItemMapper;

    @Resource
    ProductInfoMapper productInfoMapper;

    @Resource
    ProductFlowMapper productFlowMapper;

    @Override
    public int updateWorkNotice(WorkNotice workNotice) {
        Integer deptid= ShiroKit.getUser().getDeptId();//获取当前用户部门id
        String orderNo=workNotice.getOrderNo();//订单编号
        OrderInfo orderInfo=orderService.selectOrderInfoByOrderNo(orderNo);
        if(workNotice.getStatus()==2){//处理完成
            if(workNotice.getWorkType()==2){//装机
                List<OrderItem> orderItems = orderItemMapper.selectListByOrderNo(orderNo);
                ProductInfo product;
                Integer replacePeriod=-1;//更换周期(yue)
                for(OrderItem orderItem:orderItems){//减库存
                    Integer pid=orderItem.getPid();//产品Id
                    Integer quantity=orderItem.getQuantity();//数量
                    productInfoMapper.updateProductInfoAmount(pid,quantity);//减少产品库存
                    product=productInfoMapper.selectById(pid);
                    if("2".equals(product.getProductType())){//超伴
                        replacePeriod=product.getReplacePeriod();
                    }
                    if("1".equals(product.getProductType())&&replacePeriod==-1){//主机且还没出现超版
                        replacePeriod=product.getReplacePeriod();
                    }
                    ProductFlow flow=new ProductFlow();
                    flow.setPid(pid);//产品id
                    flow.setBizType(2);//1-入库 2-售出 3-出库
                    flow.setDeptid(deptid);//部门id
                    flow.setProductModel(orderItem.getProductModel());//产品型号
                    flow.setRemarks("售出");
                    flow.setAmount(quantity);
                    flow.setCreateTime(new Date());
                    productFlowMapper.insert(flow);
                }
                if(workNotice.getDealTime()==null){
                    workNotice.setDealTime(new Date());
                }
                orderInfo.setReplacePeriod(replacePeriod);
                orderInfo.setNextNiticeTime(DateUtil.getAfterMonthDate(workNotice.getDealTime(),replacePeriod));
            }

            if(workNotice.getWorkType()==1){//预埋管道
                orderInfo.setStatus(2);//待安装
            }else if(workNotice.getWorkType()==2){//装机
                orderInfo.setStatus(3);//订单完成
            }
            orderInfoMapper.updateById(orderInfo);//更新订单状态
        }
        this.workNoticeMapper.updateById(workNotice);
        return 0;
    }
}
